package Run;

import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import Chess.BorderPane;
import Chess.IPlogion;
import Chess.Main;
import Chess.MoveStrategy;
import Chess.chessPiece;
import Chess.chessType;
import Thread.ChangeThreadsend;

public class whitePawnStrategy implements MoveStrategy {
    //移动次数
    int whitemoveTimes = 0;
    Stage stage = new Stage();
    @Override
    public boolean move(chessPiece currentChessPiece, int[][] chess,int x,int y) {
    	//chessPiece chess = new chessPiece();
        //下一个坐标无棋子，进行移动
        if(chess[y][x]!=2){
            //第一次移动
            if(whitemoveTimes==0){
                if(currentChessPiece.getX()==x && currentChessPiece.getY()-y<=2){
                    currentChessPiece.move(x,y);
                    whitemoveTimes++;
                    return true;
                }
            }
            else{
                if(currentChessPiece.getX()==x && currentChessPiece.getY()-y==1){
                    currentChessPiece.move(x,y);
                    //到达底部，升级
                    if(y==0)
                        changeType(currentChessPiece,x,y);
                    return true;
                }
            }
        }
        //下一个坐标有棋子，吃子
        else{
            //符合吃子位置
            if((Math.abs(x-currentChessPiece.getX())==1 && currentChessPiece.getY()-y==1)
            ||(Math.abs(x-currentChessPiece.getX())==1 && currentChessPiece.getY()-y==2 &&whitemoveTimes ==0)){
                currentChessPiece.move(x,y);
                if(y==0)
                    changeType(currentChessPiece,x,y);
                //返回成功吃到子
                return true;
            }
        }
        return false;
    }

    public void changeType(chessPiece piece,int x,int y){
    	Pane pane = new Pane();
    	Text text = new Text("请输入点击想要变成的棋子");
    	text.setLayoutX(0);
    	text.setLayoutY(20);
    	Image image1 = new Image("images/whiteRook.png");
    	ImageView imageview1 = new ImageView(image1);
    	imageview1.setLayoutX(20);
    	imageview1.setLayoutY(40);
    	imageview1.setFitWidth(90);
    	imageview1.setFitHeight(90);
    	imageview1.setOnMouseClicked(new EventHandler<MouseEvent>(){
			public void handle(MouseEvent e){
    			piece.getImageView().setImage(null);
    			for(int k =0;k<BorderPane.getWhiteChessList().size();k++){
	                chessPiece piece1 = (chessPiece) BorderPane.getWhiteChessList().get(k);
	                if(piece.equals(piece1)){
	                    BorderPane.getWhiteChessList().remove(k);
	                    break;
	                }
	            }
    			chessPiece piece = new chessPiece(x,y,chessType.whiteRook,new RookStrategy());
    	        BorderPane.getWhiteChessList().add(piece);
    	        BorderPane.reshapeImageView(x,y,piece);
    	        Main.pane.getChildren().add(piece.getImageView());
    	        if(Main.getj()==true){
    	        new Thread(new ChangeThreadsend(IPlogion.ipwhite,1)).start();
    	        }
    	        stage.close();
    	        Main.back.setDisable(true);
    		}
    	});
    	
    	Image image2 = new Image("images/whiteQueen.png");
    	ImageView imageview2 = new ImageView(image2);
    	imageview2.setLayoutX(110);
    	imageview2.setLayoutY(40);
    	imageview2.setFitWidth(90);
    	imageview2.setFitHeight(90);
    	imageview2.setOnMouseClicked(new EventHandler<MouseEvent>(){
			public void handle(MouseEvent e){
    			piece.getImageView().setImage(null);
    			for(int k =0;k<BorderPane.getWhiteChessList().size();k++){
	                chessPiece piece1 = (chessPiece) BorderPane.getWhiteChessList().get(k);
	                if(piece.equals(piece1)){
	                    BorderPane.getWhiteChessList().remove(k);
	                    break;
	                }
	            }
    			chessPiece piece = new chessPiece(x,y,chessType.whiteQueen,new QueenStrategy());
    	        BorderPane.getWhiteChessList().add(piece);
    	        BorderPane.reshapeImageView(x,y,piece);
    	        Main.pane.getChildren().add(piece.getImageView());
    	        if(Main.getj()==true){
        	        new Thread(new ChangeThreadsend(IPlogion.ipwhite,2)).start();
        	        }
    	        stage.close();   	        
    	        Main.back.setDisable(true);
    		}
    	});
    	
    	Image image3 = new Image("images/whiteKnight.png");
    	ImageView imageview3 = new ImageView(image3);
    	imageview3.setLayoutX(200);
    	imageview3.setLayoutY(40);
    	imageview3.setFitWidth(90);
    	imageview3.setFitHeight(90);
    	imageview3.setOnMouseClicked(new EventHandler<MouseEvent>(){
			public void handle(MouseEvent e){
    			piece.getImageView().setImage(null);
    			for(int k =0;k<BorderPane.getWhiteChessList().size();k++){
	                chessPiece piece1 = (chessPiece) BorderPane.getWhiteChessList().get(k);
	                if(piece.equals(piece1)){
	                    BorderPane.getWhiteChessList().remove(k);
	                    break;
	                }
	            }
    			chessPiece piece = new chessPiece(x,y,chessType.whiteKnight,new KnightStrategy());
    	        BorderPane.getWhiteChessList().add(piece);
    	        BorderPane.reshapeImageView(x,y,piece);
    	        Main.pane.getChildren().add(piece.getImageView());
    	        if(Main.getj()==true){
        	        new Thread(new ChangeThreadsend(IPlogion.ipwhite,3)).start();
        	        }
    	        stage.close();    	        
    	        Main.back.setDisable(true);
    		}
    	});
    	
    	Image image4 = new Image("images/whiteBishop.png");
    	ImageView imageview4 = new ImageView(image4);
    	imageview4.setLayoutX(290);
    	imageview4.setLayoutY(40);
    	imageview4.setFitWidth(90);
    	imageview4.setFitHeight(90);
    	imageview4.setOnMouseClicked(new EventHandler<MouseEvent>(){
			public void handle(MouseEvent e){
    			piece.getImageView().setImage(null);
    			for(int k =0;k<BorderPane.getWhiteChessList().size();k++){
	                chessPiece piece1 = (chessPiece) BorderPane.getWhiteChessList().get(k);
	                if(piece.equals(piece1)){
	                    BorderPane.getWhiteChessList().remove(k);
	                    break;
	                }
	            }
    			chessPiece piece = new chessPiece(x,y,chessType.whiteBishop,new BishopStrategy());
    	        BorderPane.getWhiteChessList().add(piece);
    	        BorderPane.reshapeImageView(x,y,piece);
    	        Main.pane.getChildren().add(piece.getImageView());
    	        if(Main.getj()==true){
        	        new Thread(new ChangeThreadsend(IPlogion.ipwhite,4)).start();
        	    }
    	        stage.close();    	        
    	        Main.back.setDisable(true);
    		}
    	});
    	
    	pane.getChildren().addAll(text,imageview1,imageview2,imageview3,imageview4);
    	Scene scene = new Scene(pane,400,150);
        stage.setScene(scene);
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.showAndWait();
    }

}
